package controls
{
import mx.core.UIComponent;
import mx.controls.Image;
import flash.display.Shape;
import mx.core.IFlexDisplayObject;

/**
 *  The skin for the thermometer's background
 *
 *  @default null
 */
[Style(name="backgroundLayer", type="Class", inherit="no")]

/**
 *  The skin for the thermometer's masked image
 *
 *  @default null
 */
[Style(name="maskedLayer", type="Class", inherit="no")]

public class Thermometer extends UIComponent
{
	
	//--------------------------------------------------------------------------
	//
	//  Variables
	//
	//--------------------------------------------------------------------------

	
	private var backgroundImage:Image;
	
	private var maskedImage:Image;

	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------

    //----------------------------------
    //  maximum
    //----------------------------------

    /**
     *  @private
     * 	maximum value of the thermometer
     */
    private var _maximum:Number = 0;

    /**
     *  @private
     */
    public function get maximum():Number
    {
        return _maximum;
    }

    /**
     *  @private
     */
    public function set maximum(value:Number):void
    {
        _maximum = value;
        invalidateDisplayList();
    }

    //----------------------------------
    //  value
    //----------------------------------

    /**
     *  @private
     * 	current value of the thermometer
     */
    private var _value:Number = 0;

    /**
     *  @private
     */
    public function get value():Number
    {
        return _value;
    }

    /**
     *  @private
     */
    public function set value(value:Number):void
    {
        _value = value;
        invalidateDisplayList();
    }

	//--------------------------------------------------------------------------
	//
	//  Overridden Methods
	//
	//--------------------------------------------------------------------------

	
	override protected function createChildren():void
	{
		super.createChildren();
		
		if (!backgroundImage)
		{
			backgroundImage = new Image();
			addChild(backgroundImage);
		}
		
		if (!maskedImage)
		{
			maskedImage = new Image();
			maskedImage.width = 20;
			maskedImage.height = 20;
			
			var myRect:Shape = new Shape();
			myRect.graphics.beginFill(0xFF0000);
			var height:int = 0;
			myRect.graphics.drawRect(0,maskedImage.height-height,maskedImage.width,height);
			myRect.graphics.endFill();
			myRect.name = "bg_red";
			
			maskedImage.mask = myRect;
			addChild(maskedImage);
		}
	}

    /**
     *  @private
     */
    override protected function updateDisplayList(unscaledWidth:Number,
                                                  unscaledHeight:Number):void
    {
        
        super.updateDisplayList(unscaledWidth, unscaledHeight);
        
        var bgClass:Class = Class(getStyle("backgroundLayer"));
        if (bgClass && backgroundImage.source != bgClass)
        {
        	var bgSkin:IFlexDisplayObject = new bgClass();
        	backgroundImage.source = bgSkin;
        	backgroundImage.height = bgSkin.height;
        	backgroundImage.width = bgSkin.width;
        	
        }
        var maskedClass:Class = Class(getStyle("maskedLayer"));
        if (maskedClass)
        {
        	var maskedSkin:IFlexDisplayObject = new maskedClass();
        	maskedImage.source = maskedSkin;
        	maskedImage.height = maskedSkin.height;
        	maskedImage.width = maskedSkin.width;
        }

		if (value > 0 && maximum > 0)
		{
			var myRect:Shape = new Shape();
			myRect.graphics.beginFill(0xFF0000);
			var height:int = maskedImage.height*(value/maximum);
			myRect.graphics.drawRect(x,maskedImage.height-height,maskedImage.width,height);
			myRect.graphics.endFill();
			
			maskedImage.mask = myRect;
		}
    }
    
    override protected function measure():void
    {
		super.measure();
		measuredMinHeight = measuredHeight = backgroundImage.height;
		measuredMinWidth = measuredWidth = backgroundImage.width;
    }
}
}